﻿#region

using System;
using System.Diagnostics.Contracts;

#endregion

namespace BTL.Technology.Framework
{
    public class Guard
    {
        private const string ShouldNotBeNullOrEmpty = "The {0} should not be null or empty!!!";
        private const string ShouldBeGreaterThanZero = "The {0} should be greater than zero!!!";

        public static void StringArgumentShouldBeNotNullOrEmpty(string source)
        {
            var errorMessage = string.Format(ShouldNotBeNullOrEmpty, source.GetType().FullName);
#if DEBUG
            Contract.Assert(!string.IsNullOrEmpty(source), errorMessage);
#endif
            if (string.IsNullOrEmpty(source))
            {
                throw new ArgumentNullException(errorMessage);
            }
        }

        public static void ArgumentShouldBeGreaterThanZero(int source)
        {
            var errorMessage = string.Format(ShouldBeGreaterThanZero, source.GetType().FullName);
#if DEBUG
            Contract.Assert(!(source <= 0), errorMessage);
#endif
            if (source <= 0)
            {
                throw new ArgumentNullException(errorMessage);
            }
        }

        public static void Assert(bool isTrue, string errorMessage)
        {
#if DEBUG
            Contract.Assert(!isTrue, errorMessage);
#endif
            if (isTrue)
            {
                throw new Exception(errorMessage);
            }
        }

        public static void MakeSureAllInstancesIsNullNot(params object[] obj)
        {
            foreach (var o in obj)
            {
                Assert(o == null, string.Format("{0} is null", o.GetType().FullName));
            }
        }
    }
}